package com.candy.security.jwt.handle;

import com.candy.common.result.R;
import com.candy.security.jwt.service.TokenService;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 自定义退出处理类 返回成功
 */
@Configuration
public class CustomLogoutSuccessHandler implements LogoutSuccessHandler
{
	@Resource
	private TokenService tokenService;

	@Resource
	private ObjectMapper objectMapper;
	/**
	 * 退出处理
	 *
	 * @return
	 */
	@Override
	public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException
	{
		System.out.println("退出成功");
		response.setContentType("application/json;charset=UTF-8");
		tokenService.delLoginUser(request);
		R jsonData = new R(HttpStatus.OK, "");
		PrintWriter out = response.getWriter();
		out.write(objectMapper.writeValueAsString(jsonData));
		out.flush();
		out.close();
	}

}
